<?php
namespace app\admin\model;

use think\Model;

class Article extends Model
{
    //方法定义关联
    public function category(){
        return $this->belongsTo('Category');
    }

    /**
     * 获取列表
     * @param array $where
     * @param array $whereAssoc
     * @return array|\PDOStatement|string|\think\Collection
     */
    public function getDetail($where=[], $whereAssoc=[]){
        //若不指定status
        if (!in_array('status', array_column($where, 0))){
            $where[] = ['status', '>=', 0];//默认获取有效的记录
        }
        $object = $this->where($where);
        //若关联条件存在
        foreach ($whereAssoc as $key => $value){
            $object = $object->hasWhere($key, $value);
        }
        return $object->select();
    }

    /**
     * 获取某个记录的信息
     * @param $id
     * @param array $where
     * @param array $whereAssoc
     * @return array|null|\PDOStatement|string|Model
     */
    public function getDetailById($id, $where=[], $whereAssoc=[]){
        $where[] = ['id', '=', $id];
        //若不指定status
        if (!in_array('status', array_column($where, 0))){
            $where[] = ['status', '>=', 0];//默认获取有效的记录
        }
        $object = $this->where($where);
        //若关联条件存在
        foreach ($whereAssoc as $key => $value) {
            $object = $object->hasWhere($key, $value);
        }
        return $object->find();
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return false|int
     */
    public function changeStatus($id, $status){
        $where[] = ['id', '=', $id];
        $data['status'] = $status;
        return $this->save($data, $where);
    }

}